/**
 * Created by zhangxiaoyu on 2016-04-09.
 */
angular.module('hyt.partnerdzCtrl',[])

    //通过站点查询账务
  .controller('SearchPartnerdzCtrl',function($scope,$state,$filter,$ionicLoading,$stateParams){
    //合作伙伴、电话、开始时间、结束时间
    $scope.Cw={
      gsmc:'',
      gsdh:'',
      kssj:'',
      jssj:'',
      gsmcdh:''
    };

      var t = new Date();
      var iToDay=t.getDate();
      var iToMon=t.getMonth();
      var iToYear=t.getFullYear();
      var iTDate=new Date(iToYear,iToMon,(iToDay-30));
      var gsmc=$stateParams.gsmc;
      var gsdh=$stateParams.gsdh;
      $scope.Cw.gsmc=gsmc;
      $scope.Cw.gsdh=gsdh;
      if ($scope.Cw.gsmc==''&&$scope.Cw.gsdh!=''){
          $scope.Cw.gsmcdh=gsdh;
      }else if ($scope.Cw.gsmc!=''&&$scope.Cw.gsdh==''){
          $scope.Cw.gsmcdh=gsmc;
      }else if($scope.Cw.gsmc!=''&&$scope.Cw.gsdh!=''){
          $scope.Cw.gsmcdh=gsmc+'/'+gsdh;
      }
      $scope.Cw.kssj=$filter('date')(iTDate,'yyyy-MM-dd');
      $scope.showStartDate = function() { //选择日期
          var optionsStart = {
              date: iTDate,
              mode: 'date',
              maxDate: new Date($scope.Cw.jssj) - 10000
          };
          datePicker.show(optionsStart, function(d) {
              if (!isNaN(d.getTime())) {  // valid date
                  $scope.$apply(function () {
                      iTDate=d;
                      $scope.Cw.kssj = $filter('date')(iTDate,'yyyy-MM-dd');
                  });
              }
          });
      };

      var endTime = new Date();
      $scope.Cw.jssj=$filter('date')(endTime,'yyyy-MM-dd');
      $scope.showEndDate = function(){
          var optionsEnd = {
              date : endTime,
              mode : 'date',
              minDate: new Date($scope.Cw.kssj) - 10000
          };
          datePicker.show(optionsEnd,function(d) {
              if (!isNaN(d.getTime())) {  // valid date
                  $scope.$apply(function () {
                      endTime=d;
                      $scope.Cw.jssj = $filter('date')(endTime,'yyyy-MM-dd');
                  });
              }
          });
      };

    //获得该公司的合作伙伴列表
    //$scope.getList=function(){
    //  $scope.$on('List.partnerList',function(){
    //     var list=PartnerFactory.getPartner();
    //     var partnerList=list.ACTION_INFO.RESULT_LIST;
    //     console.log(partnerList);
    //  })
    //};

    //点击查询按钮传递参数并跳转到partnerdzlist页面
    $scope.queryCw=function(gsmc,gsdh,kssj,jssj){
        if(gsmc==''||gsdh=='') {
            $ionicLoading.show({
                noBackdrop: true,
                template: "请选择合作伙伴名称和电话",
                duration: 1500
            });
        }else{
            $state.go('partnerdzlist',{gsmc:gsmc,gsdh:gsdh,kssj:kssj,jssj:jssj});
        }
    }
  })
  .controller('PartnerdzListCtrl',function($scope,PartnerdzListFactory,$stateParams,Storage){
    $scope.showloading = true;
      var user_id=Storage.get('user_id');
      var yh_id=Storage.get('yh_id');
      var sid=Storage.get('sid');
      var dw_id=Storage.get('dw_id');
    //下拉刷新
    $scope.doRefresh = function(){
        PartnerdzListFactory.queryList($stateParams.gsmc,$stateParams.gsdh,$stateParams.kssj,$stateParams.jssj,sid,yh_id,user_id);
      $scope.$broadcast('scroll.refreshComplete');
    };

    //上拉加载更多
    $scope.loadMore = function(){
        PartnerdzListFactory.getMoreCaiwu($stateParams.gsmc,$stateParams.gsdh,$stateParams.kssj,$stateParams.jssj,sid,yh_id,user_id);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    };

    //下一页
    $scope.hasNextPage =function(){
      return PartnerdzListFactory.hasNextPage();
    };
      $scope.gsmc=$stateParams.gsmc;
      $scope.gsdh=$stateParams.gsdh;
    //初始化合作伙伴财务列表
    PartnerdzListFactory.queryList($stateParams.gsmc,$stateParams.gsdh,$stateParams.kssj,$stateParams.jssj,sid,yh_id,user_id);
    $scope.$on('PartnerdzList.partnerdzUpdated', function() {
      var partnerdzListFactory=PartnerdzListFactory.getCaiwu();
      $scope.partnerdzData = partnerdzListFactory.data;
      console.log($scope.partnerdzData);
      $scope.showloading=false;
    });
  })
    //选择合作伙伴
    .controller('PartnerCtrl',function($scope,$ionicLoading,$state,Storage,PartnerFactory,$ionicHistory,$stateParams,$ionicPopup,$window,$ionicTabsDelegate){
        $scope.showloading=true;
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var dw_id=Storage.get('dw_id');
        var gsmc='';
        var gsdh='';
        PartnerFactory.getPartner(sid,user_id,yh_id,dw_id,gsmc,gsdh);
        $scope.$on('PartnerList.partnerUpdated',function(){
            var result=PartnerFactory.getpar();
            $scope.partnerData=result.data;
            $scope.showloading=false;
        });
        //下拉更新
        $scope.doRefresh=function(){
            PartnerFactory.getPartner(sid,user_id,yh_id,dw_id,gsmc,gsdh);
            $scope.$broadcast('scroll.refreshComplete');
        };
        //上拉更新
        $scope.loadMore=function(){
            PartnerFactory.getMorePar(sid,user_id,yh_id,dw_id,gsmc,gsdh);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        //下一页
        $scope.hasNextPage = function() {
            return PartnerFactory.hasNextPage();
        };

        $scope.search={
            content:''
        };

        $scope.searchPar=function(val){
            var re = /^[0-9]+.?[0-9]*$/;
            if (!re.test(val)){
                gsmc=val;
            }else {
                gsdh=val;
            }
            PartnerFactory.getPartner(sid,user_id,yh_id,dw_id,gsmc,gsdh);
        };

        $scope.choosePartner=function(gsmc,gsdh){
            $state.go('searchPartnerdz',{gsmc:gsmc,gsdh:gsdh});
        };
        $scope.callPhone = function (mobilePhone) {
            var confirmPopup = $ionicPopup.confirm({
                title:mobilePhone,
                okText:'呼叫',
                cancelText:'取消'
            });
            confirmPopup.then(function(res) {
                if (res) {
                    $window.location.href = "tel:" + mobilePhone;
                }else {

                }
            });

        };
        $scope.dragRightPartner=function(){
            $ionicTabsDelegate.select(2);
        };
    });


